package cn.sanii.vx_server.controller;

import cn.sanii.vx_server.bean.Children;
import cn.sanii.vx_server.bean.DataTvBean;
import cn.sanii.vx_server.bean.Result;
import cn.sanii.vx_server.service.IHandler;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: SAnBlog
 * @create: 2020/7/10
 * Description:
 */
@Controller
@Slf4j
public class DataTvController {

    /**
     * guava 缓存
     */
    public final static Cache<String, List<DataTvBean>> CACHE = CacheBuilder.newBuilder()
            .maximumSize(100) // 设置缓存的最大容量
            .expireAfterWrite(30, TimeUnit.MINUTES) // 设置缓存时效
            .concurrencyLevel(10) // 设置并发级别为10
            .recordStats() // 开启缓存统计
            .build();

    //默认每个标签条数
    private static final int MAX_SIZE = 10;
    @Resource
    private List<IHandler> handlers;


    @GetMapping("/dataTv")
    @ResponseBody
    public List<DataTvBean> data() {
        try {
            return CACHE.get("dataTv", () -> {
                List<DataTvBean> dataTvBeans = Lists.newArrayList();
                log.info("dataTv cache invalid");
                for (IHandler handler : handlers) {
                    try {
                        List<Result> resultList = (List<Result>) handler.handler("");
                        if (CollectionUtils.isNotEmpty(resultList)) {
                            List<Children> childrenList = Lists.newArrayList();
                            resultList = chcekMax(resultList);
                            AtomicInteger index = new AtomicInteger(resultList.size());
                            resultList.forEach(result -> {
                                Children children = Children.builder().name(result.getTitle()).value(index.getAndDecrement()).build();
                                childrenList.add(children);
                            });
                            DataTvBean dataTvBean = DataTvBean.builder().name(resultList.get(0).getName()).children(childrenList).build();
                            dataTvBeans.add(dataTvBean);
                        }
                    } catch (Exception e) {
                        log.error("dataTv error:{}", e);
                    }
                }
                return dataTvBeans;
            });
        } catch (ExecutionException e) {
            log.error("DataTvController dataTv error", e);
        }
        return Lists.newArrayList();
    }

    private List<Result> chcekMax(List<Result> resultList) {
        if (resultList.size() > MAX_SIZE){
            return resultList.subList(0,MAX_SIZE);
        }
        return resultList;
    }
}
